home *** CD-ROM | disk | FTP | other *** search
- #include "SpriteTools.h"
-
- WindowPtr myWindow;
- SpritePtr gSpriteList = nil;
- GrafPtr gOffScreen, gBackScreen;
-
-
- #include <QDOffScreen.h>
-
- void MyNewGWorld(GrafPtr *offscreenGWorld, Rect *boundsRect)
- {
- GDHandle saveGD;
- GWorldPtr savePort;
-
- GetGWorld(&savePort, &saveGD);
-
- if ( noErr != NewGWorld((GWorldPtr *)offscreenGWorld, 0, boundsRect, nil, nil, pixelsLocked) )
- DoError;
- if ( LockPixels((*(CGrafPtr *)offscreenGWorld)->portPixMap) )
- ;
- SetGWorld(savePort, saveGD);
- };
-
-
-
- GrafPtr LoadFaceFromCicn(short cicnId)
- {
- GrafPtr offscreenGWorld;
- CIconHandle theCicn;
- GDHandle saveGD;
- GWorldPtr savePort;
-
- GetGWorld(&savePort, &saveGD);
- theCicn = GetCIcon(cicnId);
- MyNewGWorld(&offscreenGWorld, &(**theCicn).iconMask.bounds);
- if ( offscreenGWorld != nil )
- {
- SetGWorld((GWorldPtr)offscreenGWorld, nil);
- PlotCIcon(&(**theCicn).iconMask.bounds, theCicn);
-
- if ( offscreenGWorld == nil )
- offscreenGWorld->clipRgn = NewRgn ();
- if ( noErr != BitMapToRegion(offscreenGWorld->clipRgn, &(**theCicn).iconMask) )
- offscreenGWorld->clipRgn = nil;
-
- DisposeCIcon(theCicn);
- }
- SetGWorld(savePort, saveGD);
- return offscreenGWorld;
- }
-
- GrafPtr LoadFaceFromText(short ResID, short Which)
- {
- FontInfo info;
- CIconHandle theCicn;
- Str255 URL;
- Rect URLRect;
- short URLWidth,URLHeight;
- GrafPtr offscreenGWorld;
- GDHandle saveGD;
- GWorldPtr URLMask;
- GWorldPtr savePort;
- RGBColor black = {0x0000,0x0000,0x0000};
- RGBColor white = {0xFFFF,0xFFFF,0xFFFF};
- URLHeight = 12;
-
-
- theCicn = GetCIcon(130);
-
- TextSize(URLHeight);
- GetFontInfo(&info);
- URLHeight = info.ascent + info.descent;
-
- GetGWorld(&savePort, &saveGD);
- GetIndString(URL, ResID, Which);
- URLWidth = TextWidth(&URL,0,255);
- SetRect(&URLRect, 0,0,URLWidth,URLHeight);
- MyNewGWorld(&offscreenGWorld, &URLRect);
- MyNewGWorld(&(GrafPtr)URLMask,&URLRect);
- if ( offscreenGWorld != nil )
- {
- SetGWorld((GWorldPtr)URLMask, nil);
- RGBForeColor(&black);
- PaintRect(&URLRect);
- RGBForeColor(&white);
- DrawString(URL);
-
- SetGWorld((GWorldPtr)offscreenGWorld, nil);
- RGBForeColor(&white);
- PaintRect(&URLRect);
- RGBForeColor(&black);
- DrawString(URL);
-
- if ( offscreenGWorld == nil )
- offscreenGWorld->clipRgn = NewRgn ();
- if ( noErr != BitMapToRegion(offscreenGWorld->clipRgn, &(**theCicn).iconMask))
- offscreenGWorld->clipRgn = nil;
- }
- SetGWorld(savePort, saveGD);
- return offscreenGWorld;
- }
-
-
- static RgnHandle gTmpRgn = nil;
-
- void PlotFace(GrafPtr theCicn, GrafPtr destPort, Point where)
- {
- GDHandle saveGD;
- GWorldPtr savePort;
- Rect bounds;
- RGBColor saveForeColor, saveBackColor;
-
- GetGWorld(&savePort, &saveGD);
- bounds = theCicn->portRect;
- OffsetRect(&bounds, where.h - bounds.left, where.v - bounds.top);
-
- if ( gTmpRgn == nil )
- gTmpRgn = NewRgn ();
- CopyRgn(theCicn->clipRgn, gTmpRgn);
- OffsetRgn(gTmpRgn, where.h, where.v);
- SetPort(destPort);
- GetForeColor(&saveForeColor);
- GetBackColor(&saveBackColor);
- ForeColor(blackColor);
- BackColor(whiteColor);
- CopyBits(&theCicn->portBits, &destPort->portBits, &theCicn->portRect, &bounds, srcCopy, gTmpRgn);
- RGBForeColor(&saveForeColor);
- RGBBackColor(&saveBackColor);
- SetGWorld(savePort, saveGD);
- }
-
- SpritePtr NewSprite()
- {
- SpritePtr who;
-
- who = (SpritePtr) NewPtr(sizeof(SpriteRecord));
- if (who == nil) return nil;
- if (gSpriteList != nil)
- {
- gSpriteList->prev = who;
- }
- who->next = gSpriteList;
- who->prev = nil;
- gSpriteList = who;
- return who;
- }
-
-
- void DisposeSprite(SpritePtr who)
- {
- if (who == nil) return;
- if (who->next != nil)
- who->next->prev = who->prev;
- if (who->prev != nil)
- who->prev->next = who->next;
- if (who == gSpriteList)
- gSpriteList = who->next;
- DisposePtr((Ptr)who);
- }
-
- Boolean KeepOnScreen(SpritePtr theSprite)
- {
- Boolean returnValue = false;
-
- if (theSprite->position.h < 0)
- {
- theSprite->position.h = 0;
- theSprite->speed.h = abs(theSprite->speed.h);
- returnValue = true;
- }
- if (theSprite->position.v < 0)
- {
- theSprite->position.v = 0;
- theSprite->speed.v = abs(theSprite->speed.v);
- returnValue = true;
- }
- if (theSprite->position.h > gOffScreen->portRect.right - theSprite->face->portRect.right)
- {
- theSprite->position.h = gOffScreen->portRect.right - theSprite->face->portRect.right;
- theSprite->speed.h = -abs(theSprite->speed.h);
- returnValue = true;
- }
- if (theSprite->position.v > gOffScreen->portRect.bottom - theSprite->face->portRect.bottom)
- {
- theSprite->position.v = gOffScreen->portRect.bottom - theSprite->face->portRect.bottom;
- theSprite->speed.v = -abs(theSprite->speed.v);
- returnValue = true;
- }
-
- return returnValue;
- }
-
-
- #ifdef _hasfixedpoint
-
- Boolean KeepOnScreenFixed(SpritePtr theSprite)
- {
- Boolean returnValue = false;
-
- if (theSprite->position.h < 0)
- {
- theSprite->position.h = 0;
- theSprite->fixedPointPosition.h = 0;
- theSprite->speed.h = abs(theSprite->speed.h);
- returnValue = true;
- }
- if (theSprite->position.v < 0)
- {
- theSprite->position.v = 0;
- theSprite->fixedPointPosition.v = 0;
- theSprite->speed.v = abs(theSprite->speed.v);
- returnValue = true;
- }
- if (theSprite->position.h > gOffScreen->portRect.right - theSprite->face->portRect.right)
- {
- theSprite->position.h = gOffScreen->portRect.right - theSprite->face->portRect.right;
- theSprite->fixedPointPosition.h = theSprite->position.h << 4;
- theSprite->speed.h = -abs(theSprite->speed.h);
- returnValue = true;
- }
- if (theSprite->position.v > gOffScreen->portRect.bottom - theSprite->face->portRect.bottom)
- {
- theSprite->position.v = gOffScreen->portRect.bottom - theSprite->face->portRect.bottom;
- theSprite->fixedPointPosition.v = theSprite->position.v << 4;
- theSprite->speed.v = -abs(theSprite->speed.v);
- returnValue = true;
- }
-
- return returnValue;
- }
- #endif
-
-
- short RectSeparate(SpritePtr theSprite, SpritePtr anotherSprite)
- {
- short distance[4], shortest, shortestDistance, i;
- Rect bounds1, bounds2;
-
- bounds1 = theSprite->face->portRect;
- OffsetRect(&bounds1, theSprite->position.h, theSprite->position.v);
-
- bounds2 = anotherSprite->face->portRect;
- OffsetRect(&bounds2, anotherSprite->position.h, anotherSprite->position.v);
-
- distance[0] = bounds2.top - bounds1.bottom;
- distance[1] = bounds2.bottom - bounds1.top;
- distance[2] = bounds2.right - bounds1.left;
- distance[3] = bounds2.left - bounds1.right;
-
- shortest = 0;
- shortestDistance = abs(distance[0]);
- for (i=1; i<4; i++)
- {
- if (abs(distance[i]) < shortestDistance)
- {
- shortest = i;
- shortestDistance = abs(distance[i]);
- }
- }
-
- switch (shortest)
- {
- case 0:
- case 1:
- theSprite->position.v += distance[shortest]; break;
- case 2:
- case 3:
- theSprite->position.h += distance[shortest]; break;
- }
- return shortest;
- }
-
-
- short Rand(short range)
- {
- short roll;
-
- roll = Random();
- return (abs(roll) % range);
- }
-
-
-
- Boolean RegionHit(SpritePtr theSprite, SpritePtr anotherSprite)
- {
- RgnHandle faceRegion1, faceRegion2;
- Boolean result;
-
- faceRegion1 = NewRgn();
- faceRegion2 = NewRgn();
-
- CopyRgn(theSprite->face->clipRgn, faceRegion1);
- OffsetRgn(faceRegion1, theSprite->position.h, theSprite->position.v);
-
- CopyRgn(anotherSprite->face->clipRgn, faceRegion2);
- OffsetRgn(faceRegion2, anotherSprite->position.h, anotherSprite->position.v);
-
- SectRgn(faceRegion1, faceRegion2, faceRegion1);
- result = !EmptyRgn(faceRegion1);
-
- DisposeRgn(faceRegion1);
- DisposeRgn(faceRegion2);
-
- return result;
- }
-
-
- void SplitVector(Point v, Point d, Point *p, Point *n)
- {
- long length2, dotProduct;
-
- length2 = d.h * d.h + d.v * d.v;
-
- dotProduct = v.h * d.h + v.v * d.v;
-
- (*p).h = d.h * dotProduct / length2;
- (*p).v = d.v * dotProduct / length2;
- (*n).h = v.h - (*p).h;
- (*n).v = v.v - (*p).v;
- }
-
-